package com.winai.launcher.override;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.animation.AlphaAnimation;

import com.yhao.floatwindow.FloatWindow;

public class VolumeWindowCountTimer extends CountDownTimer {
    private Context context;
    private static long millisInFuture;
    @SuppressLint("StaticFieldLeak")
    private static VolumeWindowCountTimer mInstance;

    public static VolumeWindowCountTimer getInstance(Context context, long millisInFuture) {
        if (mInstance == null) {
            synchronized (VolumeWindowCountTimer.class) {
                if (mInstance == null) {
                    mInstance = new VolumeWindowCountTimer(millisInFuture, 1000, context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 参数 millisInFuture       倒计时总时间（如60S，120s等）
     * 参数 countDownInterval    渐变时间（每次倒计1s）
     */
    private VolumeWindowCountTimer(long millisInFuture, long countDownInterval, Context context) {
        super(millisInFuture, countDownInterval);
        VolumeWindowCountTimer.millisInFuture = millisInFuture;
        this.context = context;
    }

    /**
     * 说明：重置计时器
     */
    public synchronized void restart() {
        if (mInstance != null) {
            mInstance.cancel();
        }
        mInstance = null;
        getInstance(this.context, VolumeWindowCountTimer.millisInFuture).start();
    }

    /**
     * 说明：释放定时器
     */
    public synchronized void release() {
        if (mInstance != null) {
            mInstance.cancel();
        }
        mInstance = null;
        //销毁
        try {
            FloatWindow.destroy();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onTick(long millisUntilFinished) {
        Log.d("进入音量弹窗倒计时：",  millisUntilFinished + "。计时器：" + mInstance);
    }

    @Override
    public void onFinish() {
        if (FloatWindow.get() != null && FloatWindow.get().isShowing()) {
            FloatWindow.get().hide();
        }
    }
}
